﻿#region License
/**	
The MIT License (MIT)

Copyright (c) 2013 Philipp Schillinger, Michael Alekseew, Andreas Braun, Steeven Zeiß

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 **/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;

namespace IndoorLocalization.UserInterface 
{
	/// <summary>
	/// Realizes a small image notification displayed in the lower right corner of the screen when the reference frame changed.
	/// </summary>
	public partial class NotificationWindow : Form {

		/// <summary>
		/// The cam of which the reference frame was taken.
		/// </summary>
		private SensorCam cam;

		/// <summary>
		/// Creates a new notification window showing the new reference frame of the given camera.
		/// </summary>
		/// <param name="cam">The camera for which the reference frame should be displayed.</param>
		public NotificationWindow(SensorCam cam) {
			InitializeComponent();
			this.cam = cam;
			imageBox.Image = cam.RefFrameGray;
			Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - Width - 15, Screen.PrimaryScreen.WorkingArea.Height);
		}

		/// <summary>
		/// Called when the window is opened.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void NotificationWindow_Load(object sender, EventArgs e) {
			ThreadPool.QueueUserWorkItem(delegate {
				while (Location.Y > Screen.PrimaryScreen.WorkingArea.Height - Height) {
					Invoke(new MethodInvoker(delegate { Location = new Point(Location.X, Location.Y - 5); }));
					Thread.Sleep(10);
				}
				Invoke(new MethodInvoker(delegate { Location = new Point(Location.X, Screen.PrimaryScreen.WorkingArea.Height - Height + 1); }));
				Thread.Sleep(3000);
				while (Location.Y < Screen.PrimaryScreen.WorkingArea.Height) {
					Invoke(new MethodInvoker(delegate { Location = new Point(Location.X, Location.Y + 2); }));
					Thread.Sleep(10);
				}
				Invoke(new MethodInvoker(Close));
			});
		}

		/// <summary>
		/// Called when the user clicks anywhere at the notification.
		/// Opens the view of the cam, showing the reference frame.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void NotificationWindow_Click(object sender, EventArgs e) {
			CamView camView = new CamView(cam, CamView.DisplayMode.REFERENCE);
			camView.Show();
		}
	}
}
